/**
 * 
 */
package denaris.core.controller;

import java.util.Vector;

import javax.microedition.lcdui.game.Sprite;

import denaris.core.TiledMap;
import denaris.core.ReflectorEmitter.ReflectorWeaponController;
import denaris.factory.ImageFactory;

/**
 * @author stkl23
 * 
 */
public abstract class AbstractReflectorControllerFeature extends AbstractControllerFeature {
	public static final int REFLECTOR_SPEED = 15;
	public static final Sprite collisionTester = new Sprite(ImageFactory.getImage("/denaris/resource/sprite/transparent.png"), 1, 1);

	public ReflectorWeaponController getReflectorController() {
		return (ReflectorWeaponController) parent;
	}

	public ReflectorPosition getHead() {
		Vector path = getReflectorController().getPath();
		return (path != null && path.size() > 0) ? (ReflectorPosition) path.lastElement() : null;
	}

	public boolean advance() {
		boolean result = false;
		ReflectorPosition head = getHead();
		if (head != null) {
			int x = head.x + getDeltaX();
			int y = head.y + getDeltaY();
			ReflectorWeaponController reflectorController = getReflectorController();
			// is the new position in scope?
			if (reflectorController.isInScope(x, y)) {
				reflectorController.getPath().addElement(new ReflectorPosition(x, y));
				result = true;
			}
		}
		return result;
	}

	public boolean collidesWith(TiledMap map) {
		ReflectorPosition head = getHead();
		collisionTester.setPosition(head.x, head.y);
		return collisionTester.collidesWith(map.getLayer(), false);
	}
	
	public abstract int getDeltaX();

	public abstract int getDeltaY();
	
	
}
